﻿Imports ControllerSigeco

Public Class frmDeposito
    'instanciamos controlador
    Dim classController As New controller_stock

    Private _idDeposito As Integer = 0

    Public Property idDeposito() As Integer
        Get
            Return _idDeposito
        End Get
        Set(value As Integer)
            _idDeposito = value
        End Set
    End Property

    Private Sub frmDeposito_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        CargarDeposito()

    End Sub

    Private Sub CargarDeposito()
        Try
            If idDeposito > 0 Then

                Dim ds As New DataTable
                ds = classController.get_depositos(oiml.dbConn, idDeposito, "", "", True)

                If Not ds Is Nothing Then
                    txtDescripcion.Text = ds.Rows(0)("descripcion").ToString()
                    txtUbicacion.Text = ds.Rows(0)("ubicacion").ToString()
                    txtActualizadopor.Text = ds.Rows(0)("usuario").ToString()
                    txtEnFecha.Text = ds.Rows(0)("actualizadoFecha_").ToString()
                    cbxHabilitado.Checked = Boolean.Parse(ds.Rows(0)("habilitado").ToString())

                    btnEliminar.Enabled = True

                Else

                End If

                txtDescripcion.Focus()

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Function ValidarCampos() As Boolean

        Dim controlCampos As Boolean = False

        If txtDescripcion.Text = "" Then
            ErrorProvider1.SetError(txtDescripcion, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(txtDescripcion, "")
        End If        '

        If txtUbicacion.Text = "" Then
            ErrorProvider1.SetError(txtUbicacion, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(txtUbicacion, "")
        End If        '

        Return controlCampos

    End Function

    Private Sub btnGrabar_Click(sender As System.Object, e As System.EventArgs) Handles btnGrabar.Click

        If ValidarCampos() Then
            Exit Sub
        End If

        If idDeposito = 0 Then
            idDeposito = classController.Insert_deposito(oiml.dbConn, txtDescripcion.Text, txtUbicacion.Text, oiml.idUsuario)
            If idDeposito > 0 Then
                MessageBox.Show("Registro grabado con exito!", "Depositos", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Me.Close()

            Else
                MessageBox.Show("Error: " & classController.MessageError.Message, "Depositos", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        Else
            If classController.Update_deposito(oiml.dbConn, idDeposito, txtDescripcion.Text, txtUbicacion.Text, oiml.idUsuario, cbxHabilitado.Checked) Then
                MessageBox.Show("Registro grabado con exito!", "Depositos", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Me.Close()

            Else
                MessageBox.Show("Error: " & classController.MessageError.Message, "Depositos", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        End If

    End Sub

    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()

    End Sub

    Private Sub btnEliminar_Click(sender As System.Object, e As System.EventArgs) Handles btnEliminar.Click

        If idDeposito > 0 Then
            If MessageBox.Show("Desea eliminar el registro seleccionado?", "Depositos", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                If classController.Delete_deposito(oiml.dbConn, idDeposito) Then
                    Me.Close()

                Else
                    MessageBox.Show("Error: " & classController.MessageError.Message, "Depositos", MessageBoxButtons.OK, MessageBoxIcon.Error)

                End If

            End If

        End If

    End Sub

End Class